import {
	toast
} from '@/common/utils/toast'
import { useI18n } from "vue-i18n";
import { useCurrentInstance } from '.'
import { getMapKey } from '@/common/request/api'

export const useNavigation = () => {

	// 上下文
	const ctx = useCurrentInstance()
	const { t } = useI18n()

	let keyType = ""
	const openNavigation = async (data : {
		location : string[]
		title ?: string
		address?: string
	}) => {
		let location = data.location
		// #ifdef HTML5 || PREVIEW
		if (!keyType) {
			try {
				const _res = await getMapKey()
				keyType = _res.data.keyType
			} catch (e) {
				//TODO handle the exception
			}
		}
		// 浏览器则打开网页版地图进行导航
		// 根据返回的地图类型显示 1腾讯/2谷歌
		if (keyType === '1') {
			ctx.$showModal({
				content: t('nearby.是否跳转至腾讯地图'),
				success: res => {
					if (res.confirm) {
						// addr:手帕口桥北铁路道口
						top && (top.location.href = `https://apis.map.qq.com/uri/v1/marker?marker=coord:${location[1]},${location[0]};title:${data.title};&referer=xxx`)
					}
				}
			})
		} else {
			ctx.$showModal({
				content: t('nearby.是否跳转至谷歌地图'),
				success: res => {
					if (res.confirm) {
						top && (top.location.href = `https://www.google.com/maps/search/?api=1&query=${data.address}`)
					}
				}
			})
		}


		// #endif
		// #ifdef APP-PLUS
		getMapAPPUrl(location[1], location[0], data.title!)
		// #endif
		// #ifdef MP-WEIXIN||MP-ALIPAY
		uni.openLocation({
			type: 'gcj02',
			latitude: parseFloat(location[1]),
			longitude: parseFloat(location[0]),
			scale: 15,
			name: data.title,
			success: result => { },
			fail: (e) => { console.log(e, 'e') },
			complete: () => { }
		})
		// #endif
		// #ifdef PRIVATE || SUBSCRIPTION
		wx.openLocation({
			latitude: parseFloat(location[0]),
			longitude: parseFloat(location[1]),
			name: data.title,
			scale: 15
		})
		// #endif
	}

	const getMapAPPUrl = (latitude : string, longitude : string, name : string) => {
		let url = ''
		const actionSheetOption = {
			title: t('nearby.selectApp'),

			cancel: t('common.取消'),

			buttons: [{ title: t('nearby.TMap') }, { title: t('nearby.BMap') }, { title: t('nearby.AMap') }]
		}
		if (plus.os.name == 'Android') {
			//判断是安卓端

			plus.nativeUI.actionSheet(actionSheetOption, (e) => {
				switch (
				e.index //下面是拼接url,不同系统以及不同地图都有不同的拼接字段
				) {
					case 1: //注意referer=xxx的xxx替换成你在腾讯地图开发平台申请的key
						url = `qqmap://map/marker?marker=coord:${latitude},${longitude};title:${name}`

						break
					case 2:
						url = `baidumap://map/marker?location=${latitude},${longitude}&title=${name}&coord_type=gcj02&src=andr.baidu.openAPIdemo`

						break
					case 3:
						url = `androidamap://viewMap?sourceApplication=appname&poiname=${name}&lat=${latitude}&lon=${longitude}&dev=0`

						break
					default:
						break
				}
				openMapApp(url)
			})
		} else {
			//iOS上获取本机是否安装了百度高德地图，需要在manifest里配置

			//在manifest.json文件app-plus->distribute->ios->urlschemewhitelist节点下添加

			//(如urlschemewhitelist:["iosamap","baidumap"])

			plus.nativeUI.actionSheet(actionSheetOption, (e) => {
				switch (e.index) {
					case 1:
						url = `qqmap://map/geocoder?coord=${latitude},${longitude}&title=${name}`

						break
					case 2:
						url = `baidumap://map/marker?location=${latitude},${longitude}&title=${name}&content=${name}&src=ios.baidu.openAPIdemo&coord_type=gcj02`

						break
					case 3:
						url = `iosamap://viewMap?sourceApplication=applicationName&poiname=${name}&lat=${latitude}&lon=${longitude}&dev=0`

						break
					default:
						break
				}
				openMapApp(url)
			})
		}
	}
	const openMapApp = (url : string) => {
		if (url != '') {
			url = encodeURI(url) //调起手机APP应用

			plus.runtime.openURL(url, () => {

				toast(t('nearby.本机未安装指定的地图应用'))
			})
		}
	}

	return {
		openNavigation
	}
}